package com.wei.czz.framework.blog.service.impl;

import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.blog.dao.BlogAuditUserStatDao;
import com.wei.czz.framework.blog.entity.BlogAuditUserStatEntity;
import com.wei.czz.framework.blog.service.BlogAuditUserStatService;

import java.util.Date;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2025-02-24 22:18:55
 * className: BlogAuditUserStatServiceImpl
 * version: 1.0
 * description:
 */
@Service("blogAuditUserStatService")
public class BlogAuditUserStatServiceImpl extends ServiceImpl<BlogAuditUserStatDao, BlogAuditUserStatEntity> implements BlogAuditUserStatService {

    private static final Logger log = LoggerFactory.getLogger(BlogAuditUserStatServiceImpl.class);

    @Override
    public void saveOrEdit(BlogAuditUserStatEntity blogAuditUserStat) {
        // 获取操作用户
        String username = SecurityUtils.getOrDefaultUsername(StringUtils.EMPTY);
        Long userId = SecurityUtils.getOrDefaultUserId(CommonEnum.ZERO.getLongValue());
        Date date = new Date();

        if (Objects.isNull(blogAuditUserStat.getId())) {
            blogAuditUserStat.setDeleteStatus(CommonEnum.ZERO.getValue())
                    .setUpdateTime(date)
                    .setUpdateUser(StringUtils.EMPTY)
                    .setUpdateUserId(CommonEnum.ZERO.getLongValue())
                    .setCreateTime(date)
                    .setCreateUser(username)
                    .setCreateUserId(userId);

            int count = baseMapper.insert(blogAuditUserStat);
            log.info("保存博客审核用户统计成功。count={}", count);
        } else {
            blogAuditUserStat.setUpdateTime(date)
                    .setUpdateUser(username)
                    .setUpdateUserId(userId);

            int count = baseMapper.updateById(blogAuditUserStat);
            log.info("更新博客审核用户统计成功。count={}", count);
        }
    }
}